#include "button_task.h"
#include "button_driver.h"
#include "base_type.h"
#include "mcu_io_config.h"
#include "stdint.h"

extern TaskHandle_t button_scanf_task_handle;
extern QueueHandle_t button_isr_quene_handle;
extern TimerHandle_t Up_button_timer_handle;//软件定时器句柄
extern TimerHandle_t Down_button_timer_handle;
extern TimerHandle_t Enter_button_timer_handle;

extern bool up_flag;
extern bool down_flag;
extern bool enter_flag;

static void button_scanf_task(void *pvParameters);

esp_err_t button_init(void)
{
    esp_err_t ret_err;

    button_isr_quene_handle   = xQueueCreate(20, sizeof(button_msg_t));
    button_event_quene_handle = xQueueCreate(10, sizeof(int));

    Up_button_timer_handle = xTimerCreate((const char*)"up_button",
                        (TickType_t)500,/* 定时器周期 500(tick) */
                        (UBaseType_t)pdFALSE,/* 单次模式 */
                        (void* )UP_Pin,/* 为每个计时器分配一个索引的唯一 ID */
                        (TimerCallbackFunction_t)button_softTimer_callback); /* 回调函数 */

    Down_button_timer_handle = xTimerCreate((const char*)"down_button",
                (TickType_t)500,/* 定时器周期 500(tick) */
                (UBaseType_t)pdFALSE,/* 单次模式 */
                (void* )DOWN_Pin,/* 为每个计时器分配一个索引的唯一 ID */
                (TimerCallbackFunction_t)button_softTimer_callback); /* 回调函数 */

    Enter_button_timer_handle = xTimerCreate((const char*)"enter_button",
                (TickType_t)500,/* 定时器周期 500(tick) */
                (UBaseType_t)pdFALSE,/* 单次模式 */
                (void* )ENTER_Pin,/* 为每个计时器分配一个索引的唯一 ID */
                (TimerCallbackFunction_t)button_softTimer_callback); /* 回调函数 */    

    xTaskCreate((TaskFunction_t)button_scanf_task,		// 任务函数
                        "button_task",				    // 任务名称
                        2016,			                // 堆栈大小，实际大小为4 * usStackDepth
                        NULL,				            // 传递给任务函数的参数
                        3,					            // 任务优先级
                        &button_scanf_task_handle 		// 任务句柄（可以理解为就是该任务本身）
                );
    if(button_scanf_task_handle == NULL)
    {
        printf("button scanf task create fail");
    }

    ret_err = ESP_button_GPIO_init();

    return ret_err;
}

static void button_scanf_task(void *pvParameters)
{
    button_msg_t button_msg;
    int pic_num;
    up_flag = 0;
    down_flag = 0;
    enter_flag = 0;
    for (;;)
    {
        xQueueReceive(button_isr_quene_handle, &button_msg, portMAX_DELAY);
        switch (button_msg.num)
        {
        case UP_Pin:
            if(!up_flag)
            {
                up_flag = 1;
                xTimerStart(Up_button_timer_handle, 0);
                pic_num = 0;
                xQueueSend(button_event_quene_handle, &pic_num, 1000);
            }
            break;
        case DOWN_Pin:
            if(!down_flag)
            {
                down_flag = 1;
                xTimerStart(Down_button_timer_handle, 0);
                pic_num = 1;
                xQueueSend(button_event_quene_handle, &pic_num, 1000);
            }
            break;
        case ENTER_Pin:
            if(!enter_flag)
            {
                enter_flag = 1;
                xTimerStart(Enter_button_timer_handle, 0);
                pic_num = 2;
                xQueueSend(button_event_quene_handle, &pic_num, 1000);
            }
            break;                    
        default:
            break;
        }
    }
}
